/**
 * Collaborative Story Development Web Application (CSDApp) 
 * Copyright Anton Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

/**
 * This class just checks that a value is numeric or can be successfully cast to
 * numeric.
 *
 * @author Anton Strack
 */
public class IsEmail extends Validator {

    /**
     * A regex pattern to to handle complex numbers
     */
    private String pattern;

    /**
     * The default constructor
     */
    public IsEmail() {
        super("IsEmail");
        this.pattern = "\\b[\\w.%+-]+@[\\w.-]+\\.[\\w]{2,4}\\b"; // checks that a value matches a a valid email structure
        //This pattern is adapted from the email regex by Jan Goyvaerts at http://www.regular-expressions.info/email.html
    }

    /**
     * The default constructor with the ability to set a different regex pattern
     * for checking numbers.
     *
     * @param pattern
     */
    public IsEmail(String pattern) {
        this();
        this.pattern = pattern;
    }

    /**
     * Get the generic error message for this validator.
     *
     * @param validatorValueDisplayName the name of the value to display to an
     * end user when there is an error. For instance Email for an email
     * ValidatorValue object.
     * @return The error message of the validator
     */
    @Override
    public String getErrorMessage(String validatorValueDisplayName) {
        return validatorValueDisplayName + " needs to be a proper email address.";
    }

    /**
     * This method will validate objects that are instantiated as Strings.
     *
     * @param value the value to validate
     * @return true if the value is valid
     */
    @Override
    protected boolean handleString(String value) {
        return value.matches(this.pattern);
    }

    /**
     * This method will validate objects that are not Strings or Numbers.
     *
     * @param value the value to validate
     * @return true if the value is valid
     */
    @Override
    protected boolean handleObject(Object value) {
        return false;
    }

    /**
     * This method will validate objects that are instantiated as a class that
     * extends java.lang.Number.
     *
     * @param value the value to validate
     * @return true if the value is valid
     */
    @Override
    protected boolean handleNumber(Number value) {
        return false;
    }
}
